package com.zymail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyBus;
import com.zymail.bean.ZyGoodImg;
import com.zymail.service.ZyBusService;
import com.zymail.service.ZyGoodImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.sql.Wrapper;
import java.util.Map;

@RestController
@RequestMapping("/bus")
public class BusController extends BaseController{
    @Autowired
    public ZyBusService busService;

    @Autowired
    public ZyGoodImgService imgService;

    /**
     * 加入购物车操作
     * @param session_key session_key
     * @param de_id 商品类别id
     * @param count 商品数量
     * @return  boolean
     */
    @PostMapping("/addbus/{session_key}/{de_id}/{count}")
    public boolean addBus(@PathVariable("session_key") String session_key, @PathVariable("de_id") Integer de_id,
                          @PathVariable("count") Integer count){
        Integer userId = get_userId(session_key);
        if(userId == 0) return false;

        ZyBus zyBus = busService.getbus(userId, de_id);
        if(zyBus == null){
            return busService.save(new ZyBus(null, userId, de_id, count, null));
        }else{
            zyBus.setCount(zyBus.getCount() + count);
            zyBus.setGoodDe(null);
            return busService.updateById(zyBus);
        }
    }

    /**
     * 删除购物车操作
     * @param session_key session_key
     * @param de_id 商品类别id
     * @param count 商品数量
     * @return  boolean
     */
    @DeleteMapping("/delbus/{session_key}/{de_id}/{count}")
    public boolean delBus(@PathVariable("session_key") String session_key, @PathVariable("de_id") Integer de_id,
                          @PathVariable("count") Integer count){
        Integer userId = get_userId(session_key);
        if(userId == 0) return false;
        ZyBus zyBus = busService.getbus(userId, de_id);
        if(zyBus == null){
            return false;
        }else{
            if(count > zyBus.getCount()) return false;
            else if(count.equals(zyBus.getCount())) return busService.removeById(zyBus);
            else {
                zyBus.setCount(zyBus.getCount() - count);
                return busService.updateById(zyBus);
            }
        }
    }

    /**
     * 得到用户的完整购物车
     * @param session_key   session_key
     * @return List<zyBus>
     */
    @GetMapping("/getbus/{session_key}")
    public List<HashMap<String, String>> getBus(@PathVariable("session_key") String session_key){
        Integer userId = get_userId(session_key);
        if (userId == 0) return null;
        List<ZyBus> busList =  busService.getallbus(userId);
        ArrayList<HashMap<String, String>> result = new ArrayList<>();

        for (ZyBus bus: busList) {
            HashMap<String, String> resultmap = new HashMap<>();

            Integer good_id = bus.getGoodDe().getGoodId();
            QueryWrapper<ZyGoodImg> wrapper = new QueryWrapper<>();
            wrapper.eq("good_id", good_id);
            wrapper.eq("type", 1);
            ZyGoodImg goodImg = imgService.getOne(wrapper);

            resultmap.put("id", bus.getId().toString());
            resultmap.put("deId", bus.getGoodDe().getId().toString());
            resultmap.put("name", bus.getGoodDe().getName());
            resultmap.put("desc", bus.getGoodDe().getDe());
            resultmap.put("count", bus.getCount().toString());
            resultmap.put("imgurl", goodImg.getUrl());
            resultmap.put("price", "" + (bus.getGoodDe().getPrice()));

            result.add(resultmap);
        }

        return result;
    }

    /**
     * 删除指定购物车列表
     * @param idList    购物车id组成的列表
     * @return  boolean
     */
    @PostMapping("/remove")
    public boolean delallbus(@RequestBody List<Integer> idList){
        return busService.removeByIds(idList);
    }
}
